// 组织机构筛选
/* eslint-disable no-restricted-syntax */
// eslint-disable-next-line consistent-return
function getOrganization(objects, departmentCode) {
    for (const item of objects || []) {
        if (item.deptCode === departmentCode) return item;
        const child = getOrganization(item.resultVOS, departmentCode);
        if (child) {
            return child;
        }
    }
}

// 空值转换
function shiftNullVal(val) {
    if (!val && typeof val !== 'undefined' && val !== 0) {
        return '';
    }
    return val;
}

// 判断当前层级是否符合薪资搜索条件
function currentLevel(amtLowest, amtHighest, item, key) {
    if (
        (amtLowest !== '' && item[key] >= amtLowest && amtHighest !== '' && amtHighest >= item[key]) ||
        (amtLowest !== '' && item[key] >= amtLowest && amtHighest === '') ||
        (amtHighest !== '' && amtHighest >= item[key] && amtLowest === '')
    ) {
        return true;
    }
    return false;
}

// 薪资条件筛选
function salaryScreening(key, amtLowest, amtHighest, data = []) {
    const resData = [];
    for (const item of data) {
        if (item.resultVOS.length > 0) {
            item.resultVOS = salaryScreening(key, amtLowest, amtHighest, item.resultVOS);
            if (item.resultVOS.length > 0) {
                resData.push(item);
            } else if (currentLevel(amtLowest, amtHighest, item, key)) {
                resData.push(item);
            }
        } else if (currentLevel(amtLowest, amtHighest, item, key)) {
            resData.push(item);
        }
    }
    return resData;
}

export default function(
    originalData = [],
    {
        departmentCode,
        encryptAverageAmtHighest = '',
        encryptAverageAmtLowest = '',
        encryptTotalPayAmtHighest = '',
        encryptTotalPayAmtLowest = '',
        encryptTotalTaxAmtHighest = '',
        encryptTotalTaxAmtLowest = ''
    }
) {
    let resData = JSON.parse(JSON.stringify(originalData));

    // 根据组织架构搜索
    if (departmentCode && originalData.length > 0) {
        const newOrganization = getOrganization(resData, departmentCode);
        resData = newOrganization ? [newOrganization] : [];
    }

    const salaryAverageAmtLowest = shiftNullVal(encryptAverageAmtLowest);
    const salaryAverageAmtHighest = shiftNullVal(encryptAverageAmtHighest);
    const salaryTotalPayAmtLowest = shiftNullVal(encryptTotalPayAmtLowest);
    const salaryTotalPayAmtHighest = shiftNullVal(encryptTotalPayAmtHighest);
    const salaryTotalTaxAmtLowest = shiftNullVal(encryptTotalTaxAmtLowest);
    const salaryTotalTaxAmtHighest = shiftNullVal(encryptTotalTaxAmtHighest);

    // 根据薪资区间来调整搜索结果
    if (salaryAverageAmtLowest !== '' || salaryAverageAmtHighest !== '') {
        resData = salaryScreening('deptAvgAmt', salaryAverageAmtLowest, salaryAverageAmtHighest, resData);
    }

    if (salaryTotalPayAmtLowest !== '' || salaryTotalPayAmtHighest !== '') {
        resData = salaryScreening('payAmt', salaryTotalPayAmtLowest, salaryTotalPayAmtHighest, resData);
    }

    if (salaryTotalTaxAmtLowest !== '' || salaryTotalTaxAmtHighest !== '') {
        resData = salaryScreening('taxAmt', salaryTotalTaxAmtLowest, salaryTotalTaxAmtHighest, resData);
    }
    return {
        overviewList: resData,
        overviewTotal: { ...resData[0] } || {},
        historySource: originalData
    };
}
